#encoding:utf-8
'''
jk409@qq.com created by 2016-7-7
所需模块：qrcode，Image
功能：二维码生成器
'''
import qrcode
from io import BytesIO   #二进制流
class QR:
    def __init__(self):
        pass

    def save(self,url,img_path):
        '''
        :param url: 网址
        :param img_path:转化成相应的二维码的路径和名称（默认当前路径）
        :return:状态码0-OK
        '''
        # 方法一
        #res=qrcode.make(url).save(img_path)
        #return res
        # 方法二
        qr=qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=10,border=4)
        qr.add_data('http://my.oschina.net/jk409/blog','t.png')
        qr.make(fit=True)
        img=qr.make_image()
        res=img.save(img_path)
        return res


    def save_bio(self, url):
        qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=6, border=4)
        #qr = qrcode.QRCode(version=4, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=1)
        qr.add_data(url,'t.png')
        qr.make(fit=True)
        img = qr.make_image()
        # 流处理
        byte_io = BytesIO()
        img.save(byte_io, 'PNG')
        byte_io.seek(0)
        return byte_io




if __name__=="__main__":
    '''
    eg.1
    byte_io=qr.save_bio('http://my.oschina.net/jk409/blog')
    #print byte_io.getvalue()
    #return send_file(byte_io, mimetype='image/png')

    eg.2
    response = make_response(byte_io.getvalue())
    response.headers['Content-Type'] = "application/octet-stream"
    # response.headers['Content-Type'] = "application/vnd.ms-exce"
    response.headers['Content-Disposition'] = "inline; filename=" + 'test' + '.png'
    '''
    qr=QR()
    qr.save('http://my.oschina.net/jk409/blog','t.png')
    #byte_io = qr.save_bio('http://my.oschina.net/jk409/blog')